﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using TierApplication;
using System.Data;
using DevExpress.Web.ASPxEditors;

public partial class System_admin_School: System.Web.UI.Page
{
    #region private
    T_SCHOOL mSchool = new T_SCHOOL();
    private const string inputQueryStringIDKey = "SchoolID";
    private const string inputQueryStringPageModeKey = "pagemode";
    private const string currentPageModeKey = "CurrentPageMode";
    private const string mDataModel = "mDataModel";

    public enum PageMode
    {
        add,
        update
    }
    private Nullable<PageMode> pm;
    #endregion



    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            pm = ViewState[currentPageModeKey] as Nullable<PageMode>;
        }
        else
        {
            this.txtSCHOOLID.Text = Page.Request.QueryString[inputQueryStringIDKey];
            this.pagemode.Value = Page.Request.QueryString[inputQueryStringPageModeKey];

            switch (this.pagemode.Value.ToLower().Trim())
            {
                case "add":
                    pm = PageMode.add;
                    break;
                case "update":
                    pm = PageMode.update;
                    break;
                default:
                    pm = PageMode.add;
                    break;
            }
            ViewState[currentPageModeKey] = pm;

            if (!string.IsNullOrEmpty(this.txtSCHOOLID.Text.Trim()))
            {
                LoadSchool(this.txtSCHOOLID.Text.Trim());
            }
        }

    }

    private void LoadSchool(String SchoolID)
    {
        T_SCHOOL mSchool = new T_SCHOOL();
        mSchool.GetModel(SchoolID);
        ViewState[mDataModel] = mSchool;
        this.txtSCHOOLNAME.Text = mSchool.SCHOOLNAME;
        this.txtSCHOOLSHORTNAME.Text = mSchool.SCHOOLSHORTNAME;
        this.txtSCHOOLADDR.Text = mSchool.SCHOOLADDR;
        this.txtSCHOOLURL.Text = mSchool.SCHOOLURL;
    }

    protected void btnSchoolSave_Click(object sender, EventArgs e)
    {
        string sSchoolID = this.txtSCHOOLID.Text;
        if (String.IsNullOrEmpty(sSchoolID))
        {

        }
        else
        {
            mSchool = (T_SCHOOL)ViewState[mDataModel];
        }

        mSchool.SCHOOLNAME = this.txtSCHOOLNAME.Text;
        mSchool.SCHOOLSHORTNAME = this.txtSCHOOLSHORTNAME.Text;
        mSchool.SCHOOLADDR = this.txtSCHOOLADDR.Text;
        mSchool.SCHOOLURL = this.txtSCHOOLURL.Text;

        if (String.IsNullOrEmpty(sSchoolID))
        {
            mSchool.SCHOOLID = TranUtils.GetSchoolID();
            txtSCHOOLID.Text = mSchool.SCHOOLID;
            mSchool.Add();
        }
        else
        {
            mSchool.SCHOOLID = this.txtSCHOOLID.Text;
            mSchool.Update();
        }
        this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.returnValue='ok';window.opener=null; window.close();</script>");
    }
}